//
//  UIViewController+XUtilExtension.m
//  ViewContrerCategary
//
//  Created by Hongwei Wang on 2019/2/12 .
//  Copyright © 2019 Hongwei Wang. All rights reserved.
//

#import "UIViewController+XUtilExtension.h"


@implementation UIViewController (XUtilExtension)


- (UINavigationBar *)navigationBar {
    if ([self isKindOfClass:[UINavigationController class]]) {
        return self.navigationBar;
    }
    return self.navigationController.navigationBar;
}

- (void)pushToViewController:(UIViewController *)vc {
    vc.hidesBottomBarWhenPushed = YES;
    if ([self isKindOfClass:[UINavigationController class]]) {
        [((UINavigationController *)self) pushViewController:vc animated:YES];
    } else {
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark ======= 递归获取当前控制器 -- 如果UINavigationController或者UITabBarController 则获取root =======
- (UIViewController *)currentVCWithVC:(UIViewController *)viewController {
    UIViewController *vc;
    if ([viewController isKindOfClass:[UINavigationController class]]) {
        UIViewController *temp = ((UINavigationController *)viewController).viewControllers.firstObject;
        vc = [self currentVCWithVC:temp];
    } else if ([viewController isKindOfClass:[UITabBarController class]]) {
        UIViewController *temp = ((UITabBarController *)viewController).selectedViewController;
        vc = [self currentVCWithVC:temp];
    } else {
        vc = viewController;
    }
    return vc;
}

#pragma mark ======= 设置item =======
- (void)setBackItem:(UIBarButtonItem *)item {
    UIViewController *vc = self;
    if (vc) {
        self.navigationItem.backBarButtonItem = item;
    }
}

- (void)setRightItem:(UIBarButtonItem *)item {
    UIViewController *vc = self;
    if (vc) {
        self.navigationItem.rightBarButtonItem = item;
    }
}

- (void)setLeftItem:(UIBarButtonItem *)item {
    UIViewController *vc = self;
    if (vc) {
        self.navigationItem.leftBarButtonItem = item;
    }
}

#pragma mark ======= 自带响应方法 =======
- (void)leftNavItemClick:(id)sender {
    
}
- (void)backNavItemClick:(id)sender {
    UIViewController *vc = self;//[self currentVC];
    if (vc) {
        [vc.navigationController popViewControllerAnimated:YES];
    }
}
- (void)rightNavItemClick:(id)sender {
    
}

#pragma mark ======= left item =======
- (void)leftItemWithCustomView:(UIView *)view {
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:view];
    [self setLeftItem:item];
}

- (void)leftItemWithTitle:(NSString *)title target:(id)target action:(SEL)action {
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:target action:action];
    [self setLeftItem:item];
}

- (void)leftItemWithTitle:(NSString *)title {
    [self leftItemWithTitle:title target:self action:@selector(leftNavItemClick:)];
}

- (void)leftItemWithImage:(UIImage *)image {
    [self leftItemWithImage:image target:self action:@selector(leftNavItemClick:)];
}

- (void)leftItemWithImage:(UIImage *)image target:(id)target action:(SEL)action {
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:target action:action];
    [self setLeftItem:item];
}

- (void)leftItemWithImage:(UIImage *)image landscapeImagePhone:(UIImage *)landscapeImagePhone {
    [self leftItemWithImage:image landscapeImagePhone:landscapeImagePhone target:self action:@selector(leftNavItemClick:)];
}

- (void)leftItemWithImage:(UIImage *)image landscapeImagePhone:(nullable UIImage *)landscapeImagePhone target:(nullable id)target action:(nullable SEL)action {
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:image landscapeImagePhone:landscapeImagePhone style:UIBarButtonItemStylePlain target:self action:@selector(leftNavItemClick:)];
    [self setLeftItem:item];
}

- (void)leftItemWithSystemStyle:(UIBarButtonSystemItem)style {
    UIViewController *vc = self;
    if (style == UIBarButtonSystemItemEdit && vc) {
        vc.navigationItem.leftBarButtonItem = vc.editButtonItem;
    } else {
        [self leftItemWithSystemStyle:style target:self action:@selector(leftNavItemClick:)];
    }
}

- (void)leftItemWithSystemStyle:(UIBarButtonSystemItem)style target:(id)target action:(SEL)action {
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:style target:target action:action];
    [self setLeftItem:item];
}

#pragma mark ======= back item =======

- (void)backItemWithCustomView:(UIView *)view {
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:view];
    [self setBackItem:item];
}

- (void)backItemWithImage:(UIImage *)image {
    [self backItemWithImage:image target:self action:@selector(backNavItemClick:)];
}

- (void)backItemWithImage:(UIImage *)image target:(nullable id)target action:(nullable SEL)action {
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:target action:action];
    [self setBackItem:item];
}

- (void)backItemWithImage:(UIImage *)image landscapeImagePhone:(nullable UIImage *)landscapeImagePhone {
     [self backItemWithImage:image landscapeImagePhone:landscapeImagePhone target:self action:@selector(backNavItemClick:)];
}

- (void)backItemWithImage:(UIImage *)image landscapeImagePhone:(nullable UIImage *)landscapeImagePhone target:(nullable id)target action:(nullable SEL)action {
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:image landscapeImagePhone:landscapeImagePhone style:UIBarButtonItemStylePlain target:target action:action];
    [self setBackItem:item];
}

- (void)backItemWithTitle:(NSString *)title {
    [self backItemWithTitle:title target:self action:@selector(backNavItemClick:)];
}

- (void)backItemWithTitle:(NSString *)title target:(nullable id)target action:(nullable SEL)action {
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:target action:action];
    [self setBackItem:item];
}

- (void)backItemWithSystemStyle:(UIBarButtonSystemItem)style {
    
    UIViewController *vc = self;
    if (style == UIBarButtonSystemItemEdit && vc) {
        vc.navigationItem.backBarButtonItem = vc.editButtonItem;
    } else {
        [self backItemWithSystemStyle:style target:self action:@selector(backNavItemClick:)];
    }
    
//    [self backItemWithSystemStyle:style target:self action:@selector(backNavItemClick:)];
}

- (void)backItemWithSystemStyle:(UIBarButtonSystemItem)style target:(nullable id)target action:(nullable SEL)action {
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:style target:target action:action];
    [self setBackItem:item];
}

#pragma mark ======= right item =======

- (void)rightItemWithCustomView:(UIView *)view {
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:view];
    [self setRightItem:item];
}

- (void)rightItemWithImage:(UIImage *)image {
    [self rightItemWithImage:image target:self action:@selector(rightNavItemClick:)];
}

- (void)rightItemWithImage:(UIImage *)image target:(nullable id)target action:(nullable SEL)action {
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:target action:action];
    [self setRightItem:item];
}

- (void)rightItemWithImage:(UIImage *)image landscapeImagePhone:(nullable UIImage *)landscapeImagePhone {
    [self rightItemWithImage:image landscapeImagePhone:landscapeImagePhone target:self action:@selector(backNavItemClick:)];
}

- (void)rightItemWithImage:(UIImage *)image landscapeImagePhone:(nullable UIImage *)landscapeImagePhone target:(nullable id)target action:(nullable SEL)action {
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:image landscapeImagePhone:landscapeImagePhone style:UIBarButtonItemStylePlain target:target action:action];
    [self setRightItem:item];
}

- (void)rightItemWithTitle:(NSString *)title {
    [self rightItemWithTitle:title target:self action:@selector(rightNavItemClick:)];
}

- (void)rightItemWithTitle:(NSString *)title target:(nullable id)target action:(nullable SEL)action {
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:target action:action];
    [self setRightItem:item];
}

- (void)rightItemWithSystemStyle:(UIBarButtonSystemItem)style {
    UIViewController *vc = self;
    if (style == UIBarButtonSystemItemEdit && vc) {
        vc.navigationItem.rightBarButtonItem = vc.editButtonItem;
    } else {
        [self rightItemWithSystemStyle:style target:self action:@selector(rightNavItemClick:)];
    }
//    [self rightItemWithSystemStyle:style target:self action:@selector(rightNavItemClick:)];
}

- (void)rightItemWithSystemStyle:(UIBarButtonSystemItem)style target:(nullable id)target action:(nullable SEL)action {
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:style target:target action:action];
    [self setRightItem:item];
}

- (void)showModelToast:(NSString *)content {
    [self showModelToast:content complete:nil];
}

- (void)showModelToast:(NSString *)content complete:(NoParamBlock)complete {
    if (!content.length) {
        if (complete) {
            complete();
        }
    }
    UIAlertController *controller = [UIAlertController alertControllerWithTitle:content message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *act = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        if (complete) {
            complete();
        }
    }];
    [controller addAction:act];
    [self presentViewController:controller animated:YES completion:nil];
}

@end
